package com.onemile.entity;

import java.util.ArrayList;
import java.util.Map;

import com.onemile.entity.model.EAddress;
import com.onemile.entity.model.ECommodity;
import com.onemile.entity.model.EOrderDetails;
import com.onemile.entity.model.EOrderDetailsAccessories;
import com.onemile.entity.model.EOrderDetailsAttribute;
import com.onemile.entity.model.EShopInfo;
import com.onemile.entity.model.EUser;

import android.R.integer;
import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.util.Log;

public class Session extends Application
{
   private static Session instance = null;

   public static synchronized Session getInstance()
   {
	  if (instance == null)
		 instance = new Session();
	  return instance;
   }

   @Override
   public void onCreate()
   {
	  super.onCreate();
	  instance = (Session) getApplicationContext();
   }

   private boolean IsDEBUG = true;
   private EUser UserInfo;
   private ArrayList<ECommodity> CommodityList;
   private int commoditycount = 0;
   private EShopInfo CurrentShop;
   private EAddress userAddress;
   private double Latitude;
   private double Longitude;

   public EAddress getAddressCache()
   {
	  return userAddress;
   }

   public void setAddressCache(EAddress userAddress)
   {
	  this.userAddress = userAddress;
   }

   public void CleanAddressCache()
   {
	  userAddress = null;
   }

   /**
    * 用于定位的地址
    */
   private String Address;

   /**
    * 用于定位的地址
    * 
    * @return
    */
   public String getAddress()
   {
	  return Address;
   }

   /**
    * 用于定位的地址
    * 
    * @param address
    */
   public void setAddress(String address)
   {
	  Address = address;
   }

   public double getLatitude()
   {
	  if (Latitude == 0)
	  {
		 SharedPreferences sp = getSharedPreferences(PubConst.Setting.Setting, Activity.MODE_PRIVATE);
		 Latitude = Double.valueOf(sp.getString(PubConst.Setting.Longitude, "31.2191099"));
	  }
	  return Latitude;
   }

   public void setLatitude(double latitude)
   {
	  Latitude = latitude;
   }

   public double getLongitude()
   {
	  if (Longitude == 0)
	  {
		 SharedPreferences sp = getSharedPreferences(PubConst.Setting.Setting, Activity.MODE_PRIVATE);
		 Longitude = Double.valueOf(sp.getString(PubConst.Setting.Latitude, "121.3506946"));
	  }
	  return Longitude;
   }

   public void setLongitude(double longitude)
   {
	  Longitude = longitude;
   }

   public EShopInfo getCurrentShop()
   {
	  return CurrentShop;
   }

   public void setCurrentShop(EShopInfo currentShop)
   {
	  CurrentShop = currentShop;
   }

   public void Logout()
   {
	  if (UserInfo != null)
	  {
		 UserInfo = null;
	  }
   }

   public float getTotalPrice()
   {
	  float actualPrice = 0;
	  try
	  {
		 int size = Session.getInstance().getCommodityList().size();
		 for (int i = 0; i < size; i++)
		 {
			int count = Session.getInstance().getCommodityList().get(i).getCount();// 数量
			float UintPrice = Session.getInstance().getCommodityList().get(i).getSpecification().getPrice();// 该规格下的商品单价
			int size2 = Session.getInstance().getCommodityList().get(i).getCommodityAccessoriesList().size();
			float accessoriesPrice = 0;
			for (int j = 0; j < size2; j++)
			{
			   accessoriesPrice += Float.valueOf(Session.getInstance().getCommodityList().get(i).getCommodityAccessoriesList().get(j).getCommodityAccessoriesPrice());
			}
			actualPrice += (UintPrice + accessoriesPrice) * count;
		 }
		 return actualPrice;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_getTotalPrice()", e);
		 }
	  }
	  return actualPrice;
   }

   /**
    * 打折
    * 
    * @param type
    *           1全单打折2某个商品打折
    * @param discount
    * @return
    */
   public float getTotalPrice(int type, float discount)
   {
	  float actualPrice = 0;
	  try
	  {
		 int size = Session.getInstance().getCommodityList().size();
		 for (int i = 0; i < size; i++)
		 {
			int count = Session.getInstance().getCommodityList().get(i).getCount();// 数量
			float UintPrice = Session.getInstance().getCommodityList().get(i).getSpecification().getPrice();// 该规格下的商品单价
			int size2 = Session.getInstance().getCommodityList().get(i).getCommodityAccessoriesList().size();
			float accessoriesPrice = 0;
			for (int j = 0; j < size2; j++)
			{
			   accessoriesPrice += Float.valueOf(Session.getInstance().getCommodityList().get(i).getCommodityAccessoriesList().get(j).getCommodityAccessoriesPrice());
			}
			if (type == 2)
			{
			   actualPrice += (UintPrice * discount + accessoriesPrice) * count;
			}
			else 
			{
			   actualPrice += (UintPrice + accessoriesPrice) * count;
			}
		 }
		 if (type==1)
		 {
		   actualPrice=actualPrice*discount;
		 }	   
		return actualPrice;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_getTotalPrice()", e);
		 }
	  }
	  return actualPrice;
   }

   /**
    * 直减
    * 
    * @param offSetPrice
    * @return
    */
   public float getTotalPrice(float offSetPrice)
   {
	  float actualPrice = 0;
	  try
	  {
		 int size = Session.getInstance().getCommodityList().size();
		 for (int i = 0; i < size; i++)
		 {
			int count = Session.getInstance().getCommodityList().get(i).getCount();// 数量
			float UintPrice = Session.getInstance().getCommodityList().get(i).getSpecification().getPrice();// 该规格下的商品单价
			int size2 = Session.getInstance().getCommodityList().get(i).getCommodityAccessoriesList().size();
			float accessoriesPrice = 0;
			for (int j = 0; j < size2; j++)
			{
			   accessoriesPrice += Float.valueOf(Session.getInstance().getCommodityList().get(i).getCommodityAccessoriesList().get(j).getCommodityAccessoriesPrice());
			}
			actualPrice += (UintPrice + accessoriesPrice) * count;
		 }
		 return actualPrice - offSetPrice;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_getTotalPrice()", e);
		 }
	  }
	  return actualPrice;
   }

   public boolean HasDifferentShopsCommodity()
   {
	  int size = CommodityList.size();
	  for (int i = 0; i < size; i++)
	  {
		 int num = 0;
		 for (int j = 0; j < size; j++)
		 {
			if (!CommodityList.get(j).getShopsId().equals(CommodityList.get(i).getShopsId()))
			{
			   return true;
			}
		 }
	  }
	  return false;
   }

   /**
    * 如果存在一个同样的商品（通过CommodityId判断），则只修改数量
    * 
    * @param ecommodity
    *           要传入CommodityId和Count
    */
   public void AddCommodity(ECommodity ecommodity)
   {
	  try
	  {
		 if (CommodityList == null)
		 {
			CommodityList = new ArrayList<ECommodity>();
		 }

		 int size = CommodityList.size();
		 for (int i = 0; i < size; i++)
		 {
			ECommodity commoditys = CommodityList.get(i);
			// 如果找到已经存在购物车中，则数量+1
			if (commoditys.getCommodityId().equals(ecommodity.getCommodityId()) && commoditys.getShopsId().equals(ecommodity.getShopsId())
				  && commoditys.getCommodityAccessoriesList() == ecommodity.getCommodityAccessoriesList())
			{
			   int newNum = ecommodity.getCount();
			   commoditys.setCount(commoditys.getCount() + newNum);
			   commoditys.setPrice(commoditys.getPrice() * commoditys.getCount());
			   CommodityList.set(i, commoditys);
			   commoditycount = commoditycount + newNum;
			   return;
			}
		 }
		 // 如果没有找到则添加到购物车

		 ECommodity modcommoditys2 = new ECommodity();
		 modcommoditys2.setCommodityId(ecommodity.getCommodityId());
		 modcommoditys2.setCommodityName(ecommodity.getCommodityName());
		 modcommoditys2.setShopsId(ecommodity.getShopsId());
		 modcommoditys2.setCount(ecommodity.getCount());
		 modcommoditys2.setPrice(ecommodity.getPrice());
		 modcommoditys2.setAttributeDetailsList(ecommodity.getAttributeDetailsList());
		 modcommoditys2.setCommodityAccessoriesList(ecommodity.getCommodityAccessoriesList());
		 modcommoditys2.setSpecification(ecommodity.getSpecification());
		 CommodityList.add(modcommoditys2);
		 commoditycount += modcommoditys2.getCount();
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_Addcommoditys(Ecommodity modcommoditys) ", e);
		 }
	  }

   }

   /**
    * 修改所有商品的数量
    * 
    * @param map
    *           map为index与数量的键值
    */
   public void UpdateCommoditys(Map<Integer, String> map)
   {
	  try
	  {
		 if (CommodityList == null)
		 {
			CommodityList = new ArrayList<ECommodity>();
		 }

		 int size = CommodityList.size();
		 for (int i = 0; i < size; i++)
		 {
			ECommodity commodity = CommodityList.get(i);

			commoditycount = commoditycount - commodity.getCount();
			int newNum = Integer.valueOf(map.get(i));
			commoditycount = commoditycount + newNum;
			commodity.setCount(newNum);
			commodity.setPrice(commodity.getPrice() * commodity.getCount());
			CommodityList.set(i, commodity);
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_Updatecommoditys(HashMap<Integer, String> map)", e);
		 }
	  }

   }

   /**
    * 修改单个商品的数量
    * 
    * @param ecommodity
    *           要传入CommodityId和Count
    */
   public void UpdateCommoditys(ECommodity ecommodity)
   {
	  try
	  {
		 if (CommodityList == null)
		 {
			CommodityList = new ArrayList<ECommodity>();
		 }
		 int size = CommodityList.size();
		 for (int i = 0; i < size; i++)
		 {
			ECommodity commoditys = CommodityList.get(i);
			if (commoditys.getCommodityId().equals(ecommodity.getCommodityId()))
			{
			   commoditycount = commoditycount - commoditys.getCount();

			   commoditys.setCount(ecommodity.getCount());
			   commoditys.setPrice(commoditys.getPrice() * commoditys.getCount());
			   CommodityList.set(i, commoditys);

			   commoditycount = commoditycount + ecommodity.getCount();
			   return;
			}
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_Updatecommoditys(Ecommodity modcommoditys)", e);
		 }
	  }

   }

   /**
    * 从购物车移除商品
    * 
    * @param ecommodity
    *           要传入CommodityId和Count
    */
   public void RemoveCommoditys(ECommodity ecommodity)
   {
	  try
	  {
		 if (CommodityList == null)
		 {
			CommodityList = new ArrayList<ECommodity>();
		 }
		 int size = CommodityList.size();
		 if (size > 0)
		 {
			for (int i = 0; i < size; i++)
			{
			   ECommodity commoditys = CommodityList.get(i);

			   if (commoditys.getCommodityId().equals(ecommodity.getCommodityId()))
			   {
				  CommodityList.remove(i);
				  commoditycount = commoditycount - commoditys.getCount();
				  return;
			   }
			}
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_Removecommoditys(Ecommodity modcommoditys)", e);
		 }
	  }

   }

   /**
    * 从购物车移除商品
    * 
    * @param index
    */
   public void RemoveCommoditys(int index)
   {
	  try
	  {
		 if (CommodityList == null)
		 {
			CommodityList = new ArrayList<ECommodity>();
		 }
		 ECommodity commoditys = CommodityList.get(index);
		 commoditycount = commoditycount - commoditys.getCount();
		 CommodityList.remove(index);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "Session_Removecommoditys(int index)", e);
		 }
	  }

   }

   public void CleanCommodityList()
   {
	  CommodityList.removeAll(CommodityList);
	  commoditycount = 0;
   }

   public int GetCommodityCount()
   {
	  return (int) commoditycount;
   }

   public boolean isCartEmpty()
   {
	  if (CommodityList == null || CommodityList.isEmpty() || CommodityList.size() == 0)
	  {
		 return true;
	  }
	  else
	  {
		 return false;
	  }

   }

   public ArrayList<ECommodity> getCommodityList()
   {
	  if (CommodityList == null)
	  {
		 CommodityList = new ArrayList<ECommodity>();
	  }
	  return CommodityList;
   }

   public void setCommodityList(ArrayList<ECommodity> _CommodityList)
   {
	  this.CommodityList = _CommodityList;
   }

   public boolean IsLogin()
   {
	  if (UserInfo != null && UserInfo.getUserName() != null && !UserInfo.getUserName().equals(""))
	  {
		 return true;
	  }
	  else
	  {
		 return false;
	  }
   }

   public EUser getUserInfo()
   {
	  return UserInfo;
   }

   public void setUserInfo(EUser userInfo)
   {
	  UserInfo = userInfo;
   }

   public boolean IsDEBUG()
   {
	  return IsDEBUG;
   }

   public void setIsDEBUG(boolean isDEBUG)
   {
	  IsDEBUG = isDEBUG;
   }
}
